{% load static %}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>生产工单分析系统</title>
    <link href="{% static 'plugins/bootstrap-5.3.0-alpha1-dist/css/bootstrap.min.css' %}" rel="stylesheet">
    <style>
        :root {
            --primary-color: #2980b9;
            --primary-light: #e1f0fa;
            --secondary-color: #2c3e50;
            --accent-color: #3498db;
            --success-color: #27ae60;
            --warning-color: #f39c12;
            --danger-color: #e74c3c;
            --text-primary: #2c3e50;
            --text-secondary: #7f8c8d;
            --bg-light: #f8f9fa;
            --border-color: #ecf0f1;
        }

        body {
            background-color: #f8fafc;
            font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
            color: var(--text-primary);
        }

        .page-header {
            margin-bottom: 1.5rem;
            padding-bottom: 1rem;
            border-bottom: 2px solid var(--primary-light);
        }

        .page-header h2 {
            font-weight: 700;
            color: var(--primary-color);
        }

        .search-container {
            background-color: #fff;
            padding: 1.5rem;
            border-radius: 0.75rem;
            box-shadow: 0 4px 15px rgba(0,0,0,0.05);
            margin-bottom: 1.5rem;
            border: 1px solid var(--border-color);
        }

        .org-card {
            background-color: #fff;
            border-radius: 0.75rem;
            box-shadow: 0 4px 15px rgba(0,0,0,0.05);
            margin-bottom: 1.5rem;
            overflow: hidden;
            transition: all 0.3s ease;
            border: 1px solid var(--border-color);
        }

        .org-header {
            background: linear-gradient(to right, var(--primary-color), var(--accent-color));
            padding: 1rem 1.5rem;
            color: white;
            font-weight: 600;
            font-size: 1.1rem;
        }

        .table-responsive {
            overflow-x: auto;
        }

        .table th {
            background-color: var(--primary-light);
            color: var(--primary-color);
            font-weight: 600;
            position: sticky;
            top: 0;
        }

        .low-rate {
            color: var(--danger-color);
            font-weight: 600;
        }

        .high-rate {
            color: var(--success-color);
        }

        .pagination {
            margin: 1.5rem 0;
        }

        .form-control, .form-select {
            border: 1px solid #dee2e6;
            border-radius: 0.5rem;
        }

        .btn {
            border-radius: 0.5rem;
        }

        .date-picker-container {
            display: flex;
            gap: 0.75rem;
            align-items: center;
        }

        @media (max-width: 992px) {
            .date-picker-container {
                flex-direction: column;
                gap: 0.5rem;
            }
        }
    </style>
</head>
<body>
<div class="container py-4">
    <div class="row">
        <div class="col-12">
            <div class="page-header">
                <h2><i class="fas fa-chart-line me-2"></i>生产工单分析</h2>
            </div>

            <!-- 搜索区域 -->
            <div class="search-container">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label small text-muted fw-medium">物品名称</label>
                        <input type="text" class="form-control" id="searchItemName"
                               placeholder="输入物品名称" value="{{ filters.item_name }}">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label small text-muted fw-medium">垛号</label>
                        <input type="text" class="form-control" id="searchDuoNumber"
                               placeholder="输入垛号" value="{{ filters.duanumber }}">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label small text-muted fw-medium">线号</label>
                        <input type="text" class="form-control" id="searchLineNumber"
                               placeholder="输入线号" value="{{ filters.line_number }}">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small text-muted fw-medium">采集时间范围</label>
                        <div class="date-picker-container">
                            <input type="date" class="form-control" id="startDate"
                                   value="{{ filters.start_time }}">
                            <span>至</span>
                            <input type="date" class="form-control" id="endDate"
                                   value="{{ filters.end_time }}">
                        </div>
                    </div>
                    <div class="col-md-1 d-flex align-items-end">
                        <div class="btn-group w-100">
                            <button type="button" class="btn btn-primary" id="searchButton">
                                <i class="fas fa-search"></i>
                            </button>
                            <button type="button" class="btn btn-light" id="resetButton">
                                <i class="fas fa-sync-alt"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 数据展示 -->
            {% if grouped_data %}
                {% for group in grouped_data %}
                    <div class="org-card">
                        <div class="org-header">
                            <i class="fas fa-warehouse me-2"></i>{{ group.org }}
                        </div>
                        <div class="table-responsive">
                            <table class="table table-hover table-bordered">
                                <thead>
                                    <tr>
                                        <th>任务单号</th>
                                        <th>物品名称</th>
                                        <th>垛号</th>

                                        <th>包装</th>
                                        <th>线号</th>
                                        <th>生产总数</th>
                                        <th>采集总数</th>
                                        <th>采集率</th>
                                        <th>采集时间</th>
                                        <th>垛号生成时间</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for record in group.page_obj %}
                                        <tr>
                                            <td>{{ record.任务单号 }}</td>
                                            <td>{{ record.物品名称 }}</td>
                                            <td>{{ record.垛号 }}</td>

                                            <td>{{ record.包装 }}</td>
                                            <td>{{ record.线号 }}</td>
                                            <td>{{ record.生产总数 }}</td>
                                            <td>{{ record.采集总数 }}</td>
                                            <td class="{% if record.采集率 < 99.5 %}low-rate{% else %}high-rate{% endif %}">
                                                {{ record.采集率|floatformat:2 }}%
                                            </td>
                                            <td>{{ record.采集时间 }}</td>
                                            <td>{{ record.垛号生成时间|default:"未知" }}</td>
                                        </tr>
                                    {% empty %}
                                        <tr><td colspan="9" class="text-center">没有符合条件的数据</td></tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- 子分页导航 -->
                        {% if group.has_pagination %}
                            <nav aria-label="Sub pagination" class="d-flex justify-content-center my-3">
                                <ul class="pagination">
                                    {% for num in group.page_obj.paginator.page_range %}
                                        {% if num > group.page_obj.number|add:"-3" and num < group.page_obj.number|add:"+3" %}
                                            {% if num == group.page_obj.number %}
                                                <li class="page-item active"><a class="page-link" href="#">{{ num }}</a></li>
                                            {% else %}
                                                <li class="page-item">
                                                    <a class="page-link"
                                                       href="?page_{{ group.org }}={{ num }}&item_name={{ filters.item_name }}&duanumber={{ filters.duanumber }}&line_number={{ filters.line_number }}&start_time={{ filters.start_time }}&end_time={{ filters.end_time }}">
                                                        {{ num }}
                                                    </a>
                                                </li>
                                            {% endif %}
                                        {% endif %}
                                    {% endfor %}
                                </ul>
                            </nav>
                        {% endif %}
                    </div>
                {% endfor %}

                <!-- 外层分页导航 -->
                <nav aria-label="Group pagination" class="d-flex justify-content-center mt-4">
                    <ul class="pagination">
                        {% if grouped_data.has_previous %}
                            <li class="page-item">
                                <a class="page-link"
                                   href="?group_page=1&item_name={{ filters.item_name }}&duanumber={{ filters.duanumber }}&line_number={{ filters.line_number }}&start_time={{ filters.start_time }}&end_time={{ filters.end_time }}">
                                    &laquo; 首页
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link"
                                   href="?group_page={{ grouped_data.previous_page_number }}&item_name={{ filters.item_name }}&duanumber={{ filters.duanumber }}&line_number={{ filters.line_number }}&start_time={{ filters.start_time }}&end_time={{ filters.end_time }}">
                                    上一页
                                </a>
                            </li>
                        {% endif %}

                        {% for num in grouped_data.paginator.page_range %}
                            {% if num == grouped_data.number %}
                                <li class="page-item active"><a class="page-link" href="#">{{ num }}</a></li>
                            {% else %}
                                <li class="page-item">
                                    <a class="page-link"
                                       href="?group_page={{ num }}&item_name={{ filters.item_name }}&duanumber={{ filters.duanumber }}&line_number={{ filters.line_number }}&start_time={{ filters.start_time }}&end_time={{ filters.end_time }}">
                                        {{ num }}
                                    </a>
                                </li>
                            {% endif %}
                        {% endfor %}

                        {% if grouped_data.has_next %}
                            <li class="page-item">
                                <a class="page-link"
                                   href="?group_page={{ grouped_data.next_page_number }}&item_name={{ filters.item_name }}&duanumber={{ filters.duanumber }}&line_number={{ filters.line_number }}&start_time={{ filters.start_time }}&end_time={{ filters.end_time }}">
                                    下一页
                                </a>
                            </li>
                            <li class="page-item">
                                <a class="page-link"
                                   href="?group_page={{ grouped_data.paginator.num_pages }}&item_name={{ filters.item_name }}&duanumber={{ filters.duanumber }}&line_number={{ filters.line_number }}&start_time={{ filters.start_time }}&end_time={{ filters.end_time }}">
                                    尾页 &raquo;
                                </a>
                            </li>
                        {% endif %}
                    </ul>
                </nav>
            {% else %}
                <div class="alert alert-info text-center py-4">
                    没有找到符合条件的数据，请尝试调整搜索条件或上传新的文件。
                </div>
            {% endif %}
        </div>
    </div>
</div>

<script src="{% static 'js/jquery-3.7.1.min.js' %}"></script>
<script>
$(document).ready(function () {
    // 搜索按钮点击事件
    $('#searchButton').click(function () {
        const itemName = $('#searchItemName').val().trim();
        const duoNumber = $('#searchDuoNumber').val().trim();
        const lineNumber = $('#searchLineNumber').val().trim();
        const startDate = $('#startDate').val();
        const endDate = $('#endDate').val();

        const params = new URLSearchParams();
        if (itemName) params.set('item_name', itemName);
        if (duoNumber) params.set('duanumber', duoNumber);
        if (lineNumber) params.set('line_number', lineNumber);
        if (startDate) params.set('start_time', startDate);
        if (endDate) params.set('end_time', endDate);

        window.location.href = `?${params.toString()}`;
    });

    // 重置按钮点击事件
    $('#resetButton').click(function () {
        $('#searchItemName, #searchDuoNumber, #searchLineNumber, #startDate, #endDate').val('');
        window.location.href = window.location.pathname;
    });

    // 设置日期控件最大值为今天
    const today = new Date().toISOString().split('T')[0];
    $('#startDate, #endDate').attr('max', today);
});
</script>
</body>
</html>